package gwtide.wizards;

import java.io.File;

import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (mpe).
 */

public class NewProjectPage extends WizardPage {
	private Text prjNmText;

	private Text gwtHomeText;

	private Text prjLcText;

	private ISelection selection;

	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public NewProjectPage(ISelection selection) {
		super("wizardPage");
		setTitle("New GWT Project");
		setDescription("This wizard creates a new GWT Project.");
		this.selection = selection;
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 3;
		layout.verticalSpacing = 9;
		Label prjNmLable = new Label(container, SWT.NULL);
		prjNmLable.setText("Project Name:");

		prjNmText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData prjNmTextGd = new GridData(GridData.FILL_HORIZONTAL);
		prjNmTextGd.horizontalSpan = 2;
		prjNmText.setLayoutData(prjNmTextGd);
		prjNmText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		Label prjLcLabel = new Label(container, SWT.NULL);
		prjLcLabel.setText("Project Location(select one directory)");

		prjLcText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData layoutdata = new GridData(GridData.FILL_HORIZONTAL);
		prjLcText.setLayoutData(layoutdata);

		Button button = new Button(container, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				prjLcText.setText(handleBrowse());
				dialogChanged();
			}
		});

		Label gwtSdkHome = new Label(container, SWT.NULL);
		gwtSdkHome.setText("GWT home");

		gwtHomeText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gwtHomeTextGd = new GridData(GridData.FILL_HORIZONTAL);
		gwtHomeText.setLayoutData(gwtHomeTextGd);
		gwtHomeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});

		Button gwtHomeBrowse = new Button(container, SWT.PUSH);
		gwtHomeBrowse.setText("Browse...");
		gwtHomeBrowse.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				gwtHomeText.setText(handleBrowse());
				dialogChanged();
			}
		});

		dialogChanged();
		setControl(container);
	}

	/**
	 * Tests if the current workbench selection is a suitable container to use.
	 */
	//
	// private void initialize() {
	// if (selection != null && selection.isEmpty() == false
	// && selection instanceof IStructuredSelection) {
	// IStructuredSelection ssel = (IStructuredSelection) selection;
	// if (ssel.size() > 1)
	// return;
	// Object obj = ssel.getFirstElement();
	// if (obj instanceof IResource) {
	// IContainer container;
	// if (obj instanceof IContainer)
	// container = (IContainer) obj;
	// else
	// container = ((IResource) obj).getParent();
	// prjNmText.setText(container.getFullPath().toString());
	// }
	// }
	// gwtHomeText.setText("new_file.mpe");
	// }
	/**
	 * Uses the standard container selection dialog to choose the new value for
	 * the container field.
	 */

	private String handleBrowse() {
		org.eclipse.swt.widgets.DirectoryDialog dlg = new DirectoryDialog(
				getShell());
		return dlg.open();
	}

	/**
	 * Ensures that both text fields are set.
	 */

	private void dialogChanged() {
		if (prjNmText.getText().length() == 0) {
			updateStatus("Project name must be specified.");
			return;
		}
		if (prjLcText.getText().length() == 0) {
			updateStatus("Project location can't be empty.");
			return;
		}

		File selDir = new File(prjLcText.getText());
		
		if (!selDir.exists()) {
			try {
				selDir.mkdirs();
			} catch (Exception e) {
				updateStatus("Can't create directory as specified '"
						+ prjLcText.getText() + "'.");
			}
		}
		
		if(!selDir.isDirectory()){
			updateStatus("Invalid directory as specified '"
					+ prjLcText.getText() + "'.");
		}

		if(!selDir.canWrite()){
			updateStatus("Can't write on directory as specified '"
					+ prjLcText.getText() + "'.");
		}
		updateStatus(null);
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	public String getPrjName() {
		return prjNmText.getText();
	}

	public String getGwtHome() {
		return gwtHomeText.getText();
	}

	public String getPrjLc() {
		return prjLcText.getText();
	}
}